from common import *

def Geturl(url,host):

    options = webdriver.ChromeOptions()
    options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
    # options.add_argument('--user-data-dir=C:/Users/yzs/AppData/Local/Google/Chrome/User Data/Default')  # 设置成用户自己的数据目录
    driver = webdriver.Chrome(chrome_options=options)

    # dirver = webdriver.Firefox()

    # driver = webdriver.PhantomJS()
    # desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()
    # desired_capabilities[
    #     "phantomjs.page.settings.userAgent"] = "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
    # desired_capabilities["phantomjs.page.settings.loadImages"] = False # 不载入图片，爬页面速度会快很多
    # driver.start_session(desired_capabilities)

    driver.get(url)
    wait = WebDriverWait(driver, 2)
    # wait.until(lambda dr:dr.find_element_by_id('article-content').is_displayed())
    wait.until(lambda dr: dr.find_element_by_class_name('vjs-poster').is_displayed())
    today = time.mktime((datetime.datetime.now() + datetime.timedelta(hours=8)).timetuple())
    result=[]
    cookie = [item["name"] + "=" + item["value"] for item in driver.get_cookies()]
    cookiestr = ';'.join(item for item in cookie)
    result.append(cookiestr)
    mysql.execute("update longzs.`longzs_re` set cookie='{}' where host='{}'".format(cookiestr,host))

    #today = (datetime.datetime.now() + datetime.timedelta(days=0)).strftime("%Y-%m-%d %H:%M:%S")
    # print(cookiestr)
    # print(driver.page_source)
    result.append(driver.page_source)
    driver.execute_script(filenr("jQuery.js"))

    swfstr=driver.execute_script("return $('video').attr('src')")
    result.append(swfstr)
    updatecacthcield('swfstr',swfstr,url)
    # print(result);
    # if re.search("(?im)Coremail",cookiestr)!=None:
    #     today = time.mktime((datetime.datetime.now() + datetime.timedelta(hours=8)).timetuple())
    #     s="update `bf_config` set update_time='{}',value='{}' where name='mail_cookie'".format(today,cookiestr)
    #     mysql.execute(s)
    #driver.save_screenshot('screen.png')
    # driver.quit()
    return

    # 执行该文件的主过程
if __name__ == '__main__':
    Geturl("https://www.itouchtv.cn/video/ae5589f548d1f765ff0a509c2c763fcf","itouchtv.cn")

